﻿using NLog;
using System;

namespace GBT.Tool.Core
{
    public class TimeManagement
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        #region 得到一周的周一和周日的日期
        /// <summary>
        /// 计算本周的周一日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetMondayDate()
        {
            return GetMondayDate(DateTime.Now);
        }

        /// <summary>
        /// 计算本周周日的日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetSundayDate()
        {
            return GetSundayDate(DateTime.Now);
        }

        /// <summary>
        /// 计算某日起始日期（礼拜一的日期）
        /// </summary>
        /// <param name="someDate">该周中任意一天</param>
        /// <returns>返回礼拜一日期，后面的具体时、分、秒和传入值相等</returns>
        public static DateTime GetMondayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Monday;
            if (i == -1) i = 6;// i值 > = 0 ，因为枚举原因，Sunday排在最前，此时Sunday-Monday=-1，必须+7=6。
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Subtract(ts);
        }

        /// <summary>
        /// 计算某日结束日期（礼拜日的日期）
        /// </summary>
        /// <param name="someDate">该周中任意一天</param>
        /// <returns>返回礼拜日日期，后面的具体时、分、秒和传入值相等</returns>
        public static DateTime GetSundayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Sunday;
            if (i != 0) i = 7 - i;// 因为枚举原因，Sunday排在最前，相减间隔要被7减。
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Add(ts);
        }

        /// <summary>
        /// 判断是不是周末/节假日
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>周末和节假日返回true，工作日返回false</returns>
        //public static async Task<bool> IsHolidayByDate(DateTime date)
        public static bool IsHolidayByDate(DateTime date)
        {
            var isHoliday = false;
            var webClient = new System.Net.WebClient();
            var PostVars = new System.Collections.Specialized.NameValueCollection
            {
                { "d", date.ToString("yyyyMMdd") }//参数
            };
            try
            {
                var day = date.DayOfWeek;
                //判断是否为周末
                if (day == DayOfWeek.Sunday || day == DayOfWeek.Saturday)
                    return true;
                //0为工作日，1为周末，2为法定节假日
                //var byteResult = await webClient.UploadValuesTaskAsync("http://tool.bitefu.net/jiari/", "POST", PostVars);//请求地址,传参方式,参数集合
                //var result = Encoding.UTF8.GetString(byteResult);//获取返回值
                //if (result == "1" || result == "2")
                //    isHoliday = true;
            }
            catch
            {
                isHoliday = false;
            }
            return isHoliday;
        }
        #endregion

        #region 获取当天的周描述和时间格式化
        /// <summary>
        /// 获取日期对应的周英文描述
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string GetDayOfWeek(DateTime dt)
        {
            string week = "";
            try
            {
                var day = dt.DayOfWeek;
                switch (day)
                {
                    case DayOfWeek.Monday:
                        week = "Monday";
                        break;
                    case DayOfWeek.Tuesday:
                        week = "Tuesday";
                        break;
                    case DayOfWeek.Wednesday:
                        week = "Wednesday";
                        break;
                    case DayOfWeek.Thursday:
                        week = "Thursday";
                        break;
                    case DayOfWeek.Friday:
                        week = "Friday";
                        break;
                    case DayOfWeek.Saturday:
                        week = "Saturday";
                        break;
                    case DayOfWeek.Sunday:
                        week = "Sunday";
                        break;
                }
                return week;
            }
            catch
            {
                throw;
            }
        }
        #endregion
    }
}
